
package com.j2eefast.bussiness.contract.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.j2eefast.bussiness.contract.entity.ContractEntity;
import com.j2eefast.bussiness.contract.service.ContractService;
import com.j2eefast.common.core.business.annotaion.BussinessLog;
import com.j2eefast.common.core.controller.BaseController;
import com.j2eefast.common.core.enums.BusinessType;
import com.j2eefast.common.core.utils.PageUtil;
import com.j2eefast.common.core.utils.ResponseData;
import com.j2eefast.common.core.utils.ToolUtil;
import com.j2eefast.common.core.utils.ValidatorUtil;
import com.j2eefast.framework.annotation.RepeatSubmit;
import com.j2eefast.framework.sys.entity.SysUserEntity;
import com.j2eefast.framework.sys.service.SysUserService;
import com.j2eefast.framework.utils.Global;
import com.j2eefast.framework.utils.UserUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.Map;


/**
 * <p></p>
 *
 * @author: zhangyuan
 * @date: 2021-04-29 14:41
 * @web: http://www.letukeji.cn
 * @version: 1.0.1
 */
@Controller
@RequestMapping("/bus/voidcontract")
public class VoidContractController extends BaseController {

	private String urlPrefix = "modules/bus/voidcontract";

	@Autowired
	private ContractService contractService;
	@Autowired
	private SysUserService sysUserService;

	/**
	 * 合同管理
	 * @return
	 */
	@GetMapping("/voidcontract")
	@RequiresPermissions("bus:voidcontract:voidcontract")
	public String contract(){
		return urlPrefix +"/voidcontract";
	}

	//资料列表
	@RequestMapping("/contractList3")
	@RequiresPermissions("bus:voidcontract:voidcontract")
	@ResponseBody
	public ResponseData contractList3(@RequestParam Map<String, Object> params) {
		PageUtil page = contractService.contractList3(params);
		return success(page);
	}

	/**
	 * 合同删除
	 */
	@RepeatSubmit
	@BussinessLog(title = "删除", businessType = BusinessType.DELETE)
	@RequestMapping(value = "/del", method = RequestMethod.POST)
	@RequiresPermissions("bus:voidcontract:del")
	@ResponseBody
	public ResponseData deleteContract(HttpServletRequest request) {
		String str = request.getParameter("ids");
		if (str != "") {
			String[] s = str.split(",");
			Long[] ids = (Long[]) ConvertUtils.convert(s,Long.class);
			return contractService.deleteContract(ids) ? success() : error("删除失败!");
		} else {
			return this.error("删除失败");
		}
	}

	/**
	 * 合同删除
	 */
	@RepeatSubmit
	@RequiresPermissions("bus:voidcontract:doDelete")
	@BussinessLog(title = "删除", businessType = BusinessType.UPDATE)
	@RequestMapping(value = "/doDelete", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData doDelete(HttpServletRequest request) {
		Long  ids=Long.parseLong(request.getParameter("id")) ;
		return contractService.doDelete(ids) ? success() : error("修改失败!");
	}

	@BussinessLog(title = "作废合同", businessType = BusinessType.EXPORT)
	@RequiresPermissions("bus:voidcontract:export")
	@PostMapping("/export")
	@ResponseBody
	public ResponseData export(@RequestParam Map<String, Object> params) throws Exception {
		String fileName =  "作废合同";
		fileName = ToolUtil.encodingExcelFilename(fileName);
		String folder = Global.getTempPath() + File.separator + "pio" + File.separator;
		List<ContractEntity> listData = contractService.findContract(params);
//内容样式策略
		WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
//垂直居中,水平居中
		contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
		contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
		contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
		contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
		contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
		contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
//设置 自动换行
		contentWriteCellStyle.setWrapped(true);
//头策略使用默认
		//excel如需下载到本地,只需要将response.getOutputStream()换成File即可(注释掉以上response代码)
		EasyExcel.write(folder + fileName, ContractEntity.class)
//				设置输出excel版本,不设置默认为xlsx
				.excelType(ExcelTypeEnum.XLS).head(ContractEntity.class)
				.sheet("作废合同")
				//设置默认样式及写入头信息开始的行数
				.useDefaultStyle(true).relativeHeadRowIndex(3)
				//这里的addsumColomn方法是个添加合计的方法,可删除
				.doWrite(listData);
		return success(fileName);
	}


}
